$(document).ready(function(){

	getAllUsulan();
	$('#btn_simpan_usulan').click(function(data){
		var no_surat=$('#no_surat').val();
		var tgl_surat=$('#tgl_surat').val();
		var perihal = $('#perihal_surat').val();
		if(no_surat==='' || tgl_surat==='' || perihal===''){
			$('#error').html("Semua Field Harus Diisi!").fadeIn(500).delay(2500).fadeOut(500);
		}else{
			$.post('controller/cont.usulan.php',{aksi_rekam_usulan:true,no_surat:no_surat,tgl_surat:tgl_surat,perihal:perihal},function(data){
				if(data.msg){
					$('#success').html('Data Berhasil Direkam').fadeIn(500).delay(2500).fadeOut(500);
        			clear_input_usulan();
				}else{
					$('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
				}
			},'json');
		}
		getAllUsulan();
		return false;
	});


	$('#btn_simpan_ubah_usulan').click(function(data){
		var id_usulan=$('#id_usulan').val();
		var no_surat=$('#no_surat').val();
		var tgl_surat=$('#tgl_surat').val();
		var perihal = $('#perihal_surat').val();
		if(no_surat==='' || tgl_surat==='' || perihal===''){
			$('#error').html("Semua Field Harus Diisi!").fadeIn(500).delay(2500).fadeOut(500);
		}else{
			$.post('controller/cont.usulan.php',{aksi_simpan_ubah_usulan:true,id_usulan:id_usulan,no_surat:no_surat,tgl_surat:tgl_surat,perihal:perihal},function(data){
				if(data.msg){
					$('#success').html('Data Berhasil Diubah').fadeIn(500).delay(2500).fadeOut(500);
        			clear_input_usulan();
				}else{
					$('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
				}
			},'json');
		}
		getAllUsulan();
		return false;
	});


	return false;
});

function clear_input_usulan(){
	$('#modale').show();
	$('#modale2').hide();
	$('.modal .modal-dialog').css({
	  	'width': '40%' 
	});
	$('#judul_modal').html('Rekam Usulan');
	$('#btn_simpan_usulan').show();
	$('#btn_simpan_ubah_usulan').hide();
	document.getElementById('id_usulan').value = "";
	document.getElementById('no_surat').value = "";
	document.getElementById('tgl_surat').value = "";
	document.getElementById('perihal_surat').value = "";
}

function ubah_usulan(id_usulan){
	$('#modale').show();
	$('#modale2').hide();
	$('#judul_modal').html('Ubah Usulan');
	$('#btn_simpan_usulan').hide();
	$('#btn_simpan_ubah_usulan').show();
	$.getJSON('controller/cont.usulan.php',{aksi_get_usulan_ubah:true,id_usulan:id_usulan}, function(data){
		if(data.msg){
			document.getElementById('id_usulan').value=data.id_usulan;
			document.getElementById('no_surat').value = data.no_surat;
			document.getElementById('tgl_surat').value = data.tgl_surat;
			document.getElementById('perihal_surat').value = data.perihal;
		}else{
			$('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
		}
	});
}

function getAllUsulan(){
	$.getJSON('controller/cont.usulan.php',{list_usulan:true},function(data){
		$('#list_usulan tbody').html('');
		$.each(data,function(index,data){
			
			var tab="<tr>";
			tab +="<td>"+data.no_surat+"</td>";
			tab +="<td>"+data.tgl_surat+"</td>";
			tab +="<td>"+data.jml+"</td>";
			tab +="<td>"+data.level_usulan+"</td>";
			tab +="<td align=\"center\"><button type=\"button\" onclick=\"hapus_usulan("+ data.id_usulan+")\" class=\"btn btn-default btn-lg\"><span class=\"glyphicon glyphicon-remove-circle\"></span></button></td>";
			tab +="<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModal\" onclick=\"ubah_usulan("+data.id_usulan+")\" class=\"btn btn-default btn-lg\"><span class=\"glyphicon glyphicon-edit\"></span></a></td>";
			tab +="<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModal\" onclick=\"insert_pegawai("+data.id_usulan+")\" class=\"btn btn-default btn-lg\" ><span class=\"glyphicon glyphicon-saved\"></span>Daftar Pegawai</a></td>";
			tab +="</tr>";
			//alert(tab);
			$('#list_usulan tbody').append(tab);
		});
	});
}

function insert_pegawai(id_usulan){
	$('#modale').hide();
	$('#modale2').show();
	$('.modal .modal-dialog').css({
	  	'width': '85%' 
	});

	get_daftar_pegawai(id_usulan)

}

function hapus_usulan(id_usulan){
	$.getJSON('controller/cont.usulan.php',{aksi_hapus_usulan:true,id_usulan:id_usulan}, function(data){
		if(data.msg){
			alert("User berhasil dihapus");
			getAllUsulan();
		}else {
			$('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
		}
	});
}

function get_daftar_pegawai(id_usulan){
	$.getJSON('controller/cont.usulan.php',{aksi_daftar_pegawai:true,id_usulan:id_usulan},function(data){
		$('#list_pegawai tbody').html('');
		var tab="";
		if(data.msg){
			$.each(data.daftar,function(index,daftar){
				tab="<tr>";
				tab +="<td>"+daftar.nip+"</td>";
				tab +="<td>"+daftar.nama+"</td>";
				tab +="<td>"+daftar.jenis_cuti+"</td>";
				tab +="<td>"+daftar.jenis_ijin+"</td>";
				tab +="<td>"+daftar.tgl_cuti+"</td>";
				tab +="<td>"+daftar.status_usulan+"</td>";
				tab +="<td align=\"center\"><button type=\"button\"  class=\"btn btn-default btn-lg\"><span class=\"glyphicon glyphicon-remove-circle\"></span></button></td>";
				tab +="<td align=\"center\"><a data-toggle=\"modal\" class=\"btn btn-default btn-lg\"><span class=\"glyphicon glyphicon-edit\"></span></a></td>";
				tab +="<td align=\"center\"><a data-toggle=\"modal\" class=\"btn btn-default btn-lg\" "+aktif+"><span class=\"glyphicon glyphicon-saved\"></span>Aktivasi</a></td>";
				tab +="</tr>";
				//alert(tab);
				$('#list_pegawai tbody').append(tab);
			});
		}else{
			tab +="<tr>"
			tab +="<td colspan=\"9\">Data Tidak Ada</td>";
			tab += "</tr>";
		}
		
		$('#list_pegawai tbody').append(tab);
	});
}
